package org.an.proj.contacts.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import butterknife.Bind;
import butterknife.ButterKnife;
import org.an.ku.base.adapter.BaseSimpleAdapter;
import org.an.ku.base.itf.OnItemClickListener;
import org.an.ku.view.MyTextView;
import org.an.proj.contacts.R;
import org.an.proj.contacts.db.dao.ContactInfoDao;
import org.an.proj.contacts.db.DBManager;
import org.an.proj.contacts.model.GroupInfo;

/**
 * 创建时间：2016年10月29日13:44 <br>
 * 作者：fuchaoyang <br>
 * 描述：分组adapter
 */
public class GroupAdapter extends BaseSimpleAdapter<GroupInfo, GroupAdapter.ViewHolder> {
  private OnItemClickListener<GroupInfo> mOnItemClickListener;

  private ContactInfoDao mContactInfoDao;

  public GroupAdapter(Context context, OnItemClickListener<GroupInfo> onItemClickListener) {
    super(context);
    mOnItemClickListener = onItemClickListener;
    mContactInfoDao = DBManager.getDaoSession().getContactInfoDao();
  }

  @Override public int getLayoutId() {
    return R.layout.item_group;
  }

  @Override public ViewHolder onCreateViewHolder(View convertView) {
    return new ViewHolder(convertView);
  }

  @Override
  public void onBindViewHolder(ViewHolder holder, final GroupInfo groupInfo, final int position) {
    holder.mTvGroupName.setText(groupInfo.getGroupName());
    long count = mContactInfoDao.queryBuilder()
        .where(ContactInfoDao.Properties.GroupId.eq(groupInfo.getId()))
        .count();
    holder.mTvCount.setText(context.getString(R.string.str_contact_count, count));
    holder.mBtnDeleteGroup.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        if (mOnItemClickListener != null) {
          mOnItemClickListener.onItemClick(position, groupInfo, v);
        }
      }
    });
  }

  static class ViewHolder {
    @Bind(R.id.iv_group) ImageView mIvGroup;
    @Bind(R.id.tv_group_name) MyTextView mTvGroupName;
    @Bind(R.id.tv_count) MyTextView mTvCount;
    @Bind(R.id.btn_delete_group) ImageView mBtnDeleteGroup;

    ViewHolder(View view) {
      ButterKnife.bind(this, view);
    }
  }
}
